clear all
set printcolor gs1, permanently
set more off
cd ""
global dirout ""
global dirobs ""
global tourism ""


use "launch_dates_clean.dta", clear
gen intro_date=mdy(intro_month, intro_day, intro_year)
keep city same intro_date
rename city city_name
set obs 45
replace city_name = "Queretaro" in 44
replace same = 0 in 44
replace city_name = "Estado de Mexico" in 45
replace same = 0 in 45
replace intro_date=20781 in 45
save "TEMP_launch_dates.dta", replace

import delimited "obs_data2.csv", encoding(ISO-8859-1)clear
drop if city=="Veracruz"| city=="Riviera Maya" | city=="Pachuca" | city=="Durango" | city=="Colima"
merge m:1 city_name using "TEMP_launch_dates.dta", nogenerate
erase "TEMP_launch_dates.dta"

split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)

gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow

gen week_intro_date=week if intro_date==all_dates
drop intro_date
bysort city_name: egen intro_date=max(week_intro_date)
format intro_date week %td 
replace num_violent_trips=0 if num_violent_trips==.

rename city_name city
collapse (sum) num* sum*, by(city week intro_date)

drop if city=="Mexico City"
append using df_weekly.dta
append using edo_mex_weekly.dta

gen month=month(week)
gen year=year(week)

merge m:1 city year month using ciudad_estado_mensual_short
drop if _merge==2
drop _merge

merge 1:1 city week using precipitation_weekly_wEDOMEX.dta
drop if _merge==2
drop _merge

merge m:1 city week using mexico_surge_weekly.dta
drop if _merge==2
drop _merge

replace intro_date=20779 if city=="Estado de Mexico"

sort city week
by city: gen trend=_n
drop if sum_fare==0
keep if week>=20548
keep if week<21157 

egen city_id=group(city)
format week %td
tsset city_id week
gen time_to_event=(week-intro_date)/7
gen EVENT=(time_to_event==0)

bysort city_id: egen min_time_to_event=min(time_to_event)
drop if intro_date==. & city!="Mexico City" & city!="Queretaro"

drop if min_time_to_event>-8 & city!="Mexico City" & city!="Queretaro"
drop min_time_to_event
drop if time_to_event==. & city!="Mexico City" & city!="Queretaro"
bysort city_id: egen max_time_to_event=max(time_to_event)

tab time_to_event, g(tte_)
forvalues i=1/125  {
replace tte_`i'=0 if city=="Mexico City" | city=="Queretaro"
}
tab city_id, g(c_)

merge m:1 city year month using "$tourism/tourism.dta"
drop if _merge==2
drop _merge
gen ln_tourism = ln(tourists_foreigners)

gen ln_num_driver_canceled=ln(num_driver_canceled)
gen ln_sum_fare=ln(sum_fare)
gen ln_sum_miles=ln(sum_miles)
gen cancel_rate=num_driver_canceled/num_trips
gen price=ln(sum_fare/sum_miles)
gen miles_per_trip = ln(sum_miles/num_trips)
replace inc_pc=ln(inc_pc)
gen ln_num_trips=ln(num_trips)
gen num_violent_trips_rate=(num_violent_trips/num_trips)*10000
sort city_id week
egen time=group(week)
gen cancel_rate_rider=num_rider_canceled/num_trips
tab time, g(time_)
tsset city_id time
gen trend2=trend*trend

did_multiplegt ln_num_trips city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend  )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.8[.4]1.2) legend(off) ytitle(Trips (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
graph export "$dirout/FigureB3a.eps", replace

did_multiplegt ln_sum_fare city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend  )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.4[.4]1.2) legend(off) ytitle(Fares (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
graph export "$dirout/FigureB3b.eps", replace

did_multiplegt price city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend  )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.2[.1]0.2) legend(off) ytitle(Fares over Miles (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
graph export "$dirout/FigureB4d.eps", replace

did_multiplegt avg_surge_multiplier city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend  )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.16[.08].24) legend(off) ytitle(Average surge multiplier, height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )		
graph export "$dirout/FigureB4e.eps", replace

cd ""

use "launch_dates_clean.dta", clear
gen intro_date=mdy(intro_month, intro_day, intro_year)
keep city same intro_date
rename city city_name
set obs 45
replace city_name = "Queretaro" in 44
replace same = 0 in 44
replace city_name = "Estado de Mexico" in 45
replace same = 0 in 45
replace intro_date=20781 in 45
gen dow=dow(intro_date) 
replace dow=7 if dow==0
replace dow=dow-1
gen intro_week = intro_date - dow
format intro_date intro_week %td 
drop intro_date
save "TEMP_launch_dates.dta", replace

import delimited "Driver_SignUps.csv", encoding(ISO-8859-1) clear
split date, p(" ")
split date1, p("-")
destring date1*, replace
rename date11 year 
rename date12 month
rename date13 day
keep city_name su year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name su week
rename city_name city
rename su driver_su
save "Driver_SignUps.dta", replace


import delimited "ETA_and_Supply_hours.csv", encoding(ISO-8859-1) clear
split week, p("-")
destring week*, replace
rename week1 year 
rename week2 month
rename week3 day
keep city_name minutes_per_driver average_eta_seconds year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name minutes_per_driver average_eta_seconds week
rename city_name city
destring average_eta_seconds, replace force
replace average_eta_seconds=average_eta_seconds/60
rename average_eta_seconds avg_eta_min
replace minutes_per_driver=minutes_per_driver/60
rename minutes_per_driver driver_hours
save "ETA_and_Supply_hours.dta", replace

import delimited "$dirobs/obs_data2.csv", encoding(ISO-8859-1)clear
split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format  week %td 
rename city_name city
collapse (sum) num_rider_canceled num_driver_canceled, by(city week)
save "Cancellation.dta", replace


import delimited "SignUps.csv", encoding(ISO-8859-1) clear
split date, p(" ")
split date1, p("-")
destring date1*, replace
rename date11 year 
rename date12 month
rename date13 day
keep city_name su year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name su week
rename city_name city
save "SignUps.dta", replace

import delimited "$dirobs/obs_data2.csv", encoding(ISO-8859-1)clear
split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
replace num_violent_trips=0 if num_violent_trips==.
rename city_name city
collapse (sum) num* sum*, by(city week)
save "$dirobs/Sessions_Requests.dta", replace
 

import excel "Active Drivers Mexico.xlsx", sheet("Sheet1") firstrow clear
drop if city=="Veracruz"| city=="Riviera Maya" | city=="Pachuca" | city=="Durango" | city=="Colima"
merge m:1 city_name using "TEMP_launch_dates.dta", nogenerate
erase "TEMP_launch_dates.dta"
gen month=month(week)
gen year=year(week)

bysort city_name: egen intro_date=max(intro_week)
format intro_date week %td 

rename city_name city
merge m:1 city year month using ciudad_estado_mensual_short, keep(match) nogenerate

collapse (sum) active* total* cash* (mean) e_rate inc_pc, by(city week intro_date)

merge 1:1 week city using "SignUps.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "Driver_SignUps.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "ETA_and_Supply_hours.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "Cancellation.dta"
drop if _merge==2
drop _merge

merge 1:1 city week using precipitation_weekly.dta
drop if _merge==2
drop _merge

merge 1:1 city week using "$dirobs/Sessions_Requests.dta"
drop if _merge==2
drop _merge

drop if  total_fares_local==0

sort city week
by city: gen trend=_n
gen trend2=trend*trend

keep if week>=20548
keep if week<21157 
egen city_id=group(city)
format week %td
egen time=group(week)
tsset city_id time
tab time, g(time_)
gen time_to_event=(week-intro_date)/7

gen EVENT=(time_to_event==0)

bysort city_id: egen min_time_to_event=min(time_to_event)
drop if intro_date==. &   city!="Queretaro"
drop if min_time_to_event>-8  & city!="Queretaro"
drop if time_to_event==.   & city!="Queretaro"
bysort city_id: egen max_time_to_event=max(time_to_event)

tab time_to_event, gen(tte_)
forvalues i=1/125  {
replace tte_`i'=0 if  city=="Queretaro"
}
tab city_id, g(c_)

gen year=year(week)
gen month=month(week)

merge m:1 city year month using "$tourism/tourism.dta"
drop if _merge==2
drop _merge
gen ln_tourism = ln(tourists_foreigners)

gen active_riders_drivers=active_riders/active_drivers
gen active_riders_trips=active_riders/total_trips
gen ln_active_riders_trips=ln(active_riders_trips)
gen ln_active_riders_drivers=ln(active_riders)-ln(active_drivers)
gen fares_per_activedriver=(total_fares_local/active_drivers)*(1/19)
gen ln_active_drivers=ln(active_drivers)
gen ln_su=ln(su)
gen ln_driver_su=ln(driver_su)
gen ln_active_riders=ln(active_riders)
gen ln_driver_hours=ln(driver_hours)
gen ln_num_driver_canceled=ln(num_driver_canceled)
gen cancel_rate=num_driver_canceled/total_trips
gen cancel_rate_rider=num_rider_canceled/total_trips
gen ln_fares_hours=ln(total_fares_local/(active_drivers*driver_hours))
gen request_session=num_requests/num_sessions
gen num_violent_trips_rate=(num_violent_trips/num_trips)*10000
gen ln_epaid_trips=ln(total_trips-cash_trips)
gen ln_epaid_fares=ln(total_fares_local-cash_fares_local)


did_multiplegt ln_epaid_trips city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks)  ylabel(-.8[.2].4) legend(off) ytitle(Trips in card (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB4c.eps", replace	

did_multiplegt ln_active_riders city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.4[.2]1)  legend(off) ytitle(Active riders (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB3c.eps", replace	

did_multiplegt ln_active_riders_drivers city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.2[.2].6)  legend(off) ytitle(Active riders over active drivers (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB4a.eps", replace

did_multiplegt ln_su city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks)  ylabel(-.8[.4]2) legend(off) ytitle(Rider sign up (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB3d.eps", replace	

did_multiplegt ln_driver_su city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.8[.4]1.2) legend(off) ytitle(Driver sign up (in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB3f.eps", replace	

did_multiplegt fares_per_activedriver city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks)  ylabel(-50[20]80) legend(off) ytitle(Fares per active driver (dollars), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB4b.eps", replace	
	
did_multiplegt ln_driver_hours city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-.4[.2].8) legend(off) ytitle(Driver hours (weekly, in logs), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB3e.eps", replace

did_multiplegt avg_eta_min city_id time EVENT, robust_dynamic  dynamic(70) placebo(35) longdiff_placebo breps(10) seed(1)  controls(e_rate inc_pc  precipitation ln_tourism trend )  graphoptions( xlabel(-30[10]70) xtitle(Weeks) ylabel(-4[2]2)  legend(off) ytitle(Avg. ETA (minutes), height(5))  title() yline(0, lcolor(black)) legend(off) xline(0, lcolor(cranberry) lwidth(thick))  graphregion(color(white)) plotregion(fcolor(white)) )
		graph export "$dirout/FigureB4f.eps", replace

 
